/*
 * TODO
 */
package com.xtrip.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.impl.Log4JLogger;

/**
 * @project xtrip
 * @author shiming.yao
 * @time 2013-6-6
 */
public class XtripLogger {
	private Log4JLogger log = null;
	private String logPath = null;
	private String fileName = null;
	private String path = null;
	private String clazz = null;
	/**
	 * 
	 */
	public XtripLogger(String clazz) {
		log = new Log4JLogger(clazz);
		logPath = System.getProperty("user.dir")+"\\log\\";
		clazz = clazz;
	}
	private Log4JLogger getLogger(Class<?> clazz){
		Log4JLogger log = new Log4JLogger(clazz.getName());
		return log;
	}
	public void info(String message){
		
		
	}
	public void start(String info){
		fileName = clazz+"_"+XtripStringUtils.formatDate3(new Date())+".log";
		path = logPath+fileName;
		log.info("\r\n==========="+info+XtripStringUtils.formatDate2(new Date())+"START ===========\r\n");
	}
	public void end(String info){
		log.info("==========="+info+XtripStringUtils.formatDate2(new Date())+"END ===========");
	}
	public void i(String info){
		log.info(info);
		write2File("INFO", info);
	}
	
	public void d(String info){
		fileName = clazz+"_"+XtripStringUtils.formatDate3(new Date())+".log";
		path = logPath+fileName;
		log.info(info);
		write2File("DEBUG", info);
	}
	public void e(String info){
		fileName = clazz+"_"+XtripStringUtils.formatDate3(new Date())+".log";
		path = logPath+fileName;
		log.info(info);
		write2File("ERROR", info);
	}
	
	private void write2File(String level,String info) {
		File f = new File(path);
		if(!f.exists()){
			try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String msg = "["+level+"]:"+info +"\r\n";
		FileWriter writer;
		try {
			writer = new FileWriter(f.getAbsolutePath(),true);
			writer.write(msg);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
